package com.gr.smartedubackend.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gr.smartedubackend.common.ErrorCode;
import com.gr.smartedubackend.constant.CommonConstant;
import com.gr.smartedubackend.model.dto.teaching_schedule.TeachingScheduleQueryRequest;
import com.gr.smartedubackend.model.entity.TeachingSchedule;
import com.gr.smartedubackend.service.TeachingScheduleService;
import com.gr.smartedubackend.mapper.TeachingScheduleMapper;
import com.gr.smartedubackend.utils.ThrowUtils;
import org.springframework.stereotype.Service;

/**
 * @author 尘轩
 * @description 针对表【teaching_schedule(授课安排)】的数据库操作Service实现
 * @createDate 2025-07-02 09:46:18
 */
@Service
public class TeachingScheduleServiceImpl extends ServiceImpl<TeachingScheduleMapper, TeachingSchedule>
        implements TeachingScheduleService {

    @Override
    public QueryWrapper<TeachingSchedule> getQueryWrapper(TeachingScheduleQueryRequest teachingScheduleQueryRequest) {

        ThrowUtils.throwIf(teachingScheduleQueryRequest == null, ErrorCode.PARAMS_ERROR);

        Long teacherId = teachingScheduleQueryRequest.getTeacherId();
        String teacherName = teachingScheduleQueryRequest.getTeacherName();
        String className = teachingScheduleQueryRequest.getClassName();
        String subject = teachingScheduleQueryRequest.getSubject();
        String weekday = teachingScheduleQueryRequest.getWeekday();
        String classroom = teachingScheduleQueryRequest.getClassroom();
        String lessonPeriod = teachingScheduleQueryRequest.getLessonPeriod();
        String term = teachingScheduleQueryRequest.getTerm();
        String sortField = teachingScheduleQueryRequest.getSortField();
        String sortOrder = teachingScheduleQueryRequest.getSortOrder();

        QueryWrapper<TeachingSchedule> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(ObjectUtil.isNotEmpty(teacherId), "teacherId", teacherId);
        queryWrapper.like(StrUtil.isNotEmpty(teacherName), "teacherName", teacherName);
        queryWrapper.like(StrUtil.isNotEmpty(className), "className", className);
        queryWrapper.like(StrUtil.isNotEmpty(subject), "subject", subject);
        queryWrapper.eq(StrUtil.isNotEmpty(weekday), "weekday", weekday);
        queryWrapper.like(StrUtil.isNotEmpty(classroom), "classroom", classroom);
        queryWrapper.eq(StrUtil.isNotEmpty(lessonPeriod), "lessonPeriod", lessonPeriod);
        queryWrapper.eq(StrUtil.isNotEmpty(term), "term", term);
        if (StrUtil.isNotEmpty(sortOrder) && StrUtil.isNotEmpty(sortField)) {
            queryWrapper.orderBy(true, sortOrder.equals("ascend"), sortField);
        } else {
            queryWrapper.orderByDesc(CommonConstant.CREATE_TIME);
        }

        return queryWrapper;
    }
}




